#include <stdio.h>
#include "../inc/uthash.h"

typedef struct {
    int id;
    UT_hash_handle hh;
} User;

User *g_userList = NULL;

void AddUser(const int id)
{
    User *tmp = NULL;
    HASH_FIND_INT(g_userList, &id, tmp);
    if (tmp) {
        printf("ADD: %d, exist\n", id);
    } else {
        tmp = (User *)malloc(sizeof(User));
        memset(tmp, 0, sizeof(User));
        tmp->id = id;
        HASH_ADD_INT(g_userList, id, tmp);
        printf("ADD: %d, add user\n", id);
    }
}

void PrintList(void)
{
    User *tmp = NULL;
    User *el = NULL;
    printf("Print cnt: %d\n", HASH_COUNT(g_userList));
    HASH_ITER(hh, g_userList, el, tmp)
    {
        printf("Print: %d\n", el->id);
    }
}

void FreeList()
{
    User *tmp = NULL;
    User *el = NULL;
    printf("Delete: %d\n", HASH_COUNT(g_userList));
    HASH_ITER(hh, g_userList, el, tmp)
    {
        printf("delete: %d\n", el->id);
        HASH_DEL(g_userList, el);
        free(el);
    }
}

int main()
{
    AddUser(3);
    AddUser(13);
    PrintList();
    FreeList();
    PrintList();
    return 0;
}